/**
 *  EKology - An Ecosystem Simulation Framework
 *  Copyright (C) 2008  Aarón Tavío - aaron.tavio at gmail.com - ULPGC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package net.ekology.core.gui.tasks;

import net.ekology.core.Fecade;
import net.ekology.core.gui.EKologyView;
import net.ekology.core.gui.datatypes.EKosystemModel;
        

import org.jdesktop.application.Application;
import org.jdesktop.application.Task;
import java.io.File;
import java.lang.reflect.*;


/**
 * @author Aarón Tavío - aaron.tavio at gmail.com
 * @version 1.0.0 - 20081019-1700
 */
public class ModelLoadTask extends Task<Void,Void> {
    // Attributes
    private EKologyView oView;
    private File oModelDirectory = null;
    private int iTotalSteps,iCurrentStep;
    
    // Methods
    public ModelLoadTask(Application oApplication, EKologyView oView, File oModelDirectory) {
        super(oApplication);
        setProgress(0);
        this.oView = oView;
        this.oModelDirectory = oModelDirectory;
        this.iCurrentStep = 0;
        this.iTotalSteps = 2;
    }
    
    @Override
    protected Void doInBackground() {
        Fecade oFecade = Fecade.getInstance();
        
        stateProgress("Checking model...");
        if (EKosystemModel.checkValidModel(oModelDirectory)) {
            stateProgress("Loading classes...");
            oView.setEKologyModel(oFecade.loadModel(oModelDirectory));
            
        }
        else stateProgress("Failed to validate model!");
        
        return (Void) null;
    }
    
    
    @Override
    public void succeeded(Void oResult) {
        setProgress(100);
        return;
    }
    
    
    private void stateProgress(String sMessage) {
        setMessage(sMessage);
        if (iCurrentStep < iTotalSteps) setProgress((iCurrentStep++*100)/iTotalSteps);
    }


}
